package com.demo.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.demo.core.filter.RequestContext;
import com.demo.persistence.entity.UserOperateLog;
import com.demo.persistence.service.MpUserOperateLogService;
import com.demo.user.converter.UserConverter;
import com.swak.common.dto.Response;
import com.swak.operatelog.OperateLogService;
import com.swak.operatelog.annotation.OperateDataLog;
import com.swak.operatelog.annotation.OperateLogContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class OperateLogServiceImpl implements OperateLogService {

    private Logger logger = LoggerFactory.getLogger("operateLog");

    @Resource
    private UserConverter userConverter;

    @Resource
    private MpUserOperateLogService mpUserOperateLogService;


//    @Override
//    public Response<Void> addOperationLogs(List<OperateDataLog> operateDataLogs) {
//        List<UserOperateLog> userOperateLogs = operateDataLogs.stream().map(item -> {
//            UserOperateLog userOperateLog = userConverter.toLogEntity(item);
//            return userOperateLog;
//        }).collect(Collectors.toList());
//         mpUserOperateLogService.saveBatch(userOperateLogs);
//         return Response.success();
//    }

    @Override
    public Response<Void> addOperationLogs(List<OperateDataLog> operateDataLogs) {
        for (OperateDataLog item : operateDataLogs) {
            UserOperateLog userOperateLog = userConverter.toLogEntity(item);
            logger.info("{} @@ {} ",userOperateLog.getUserId(),JSON.toJSONString(userOperateLog));
        }
        return Response.success();
    }


    @Override
    public void preHandle(OperateLogContext contextHolder) {
        contextHolder.setUserId(RequestContext.getUserId());
    }
}
